import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import local from '../utils/local.js'
import store from '../store'
import { getLoginStatus } from '@/utils/auth'
import { _local } from '@/utils/storage'

// 处理官方未异常捕获
// push
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, resolve, reject) {
  if (resolve || reject) return originalPush.call(this, location, resolve, reject)
  return originalPush.call(this, location).catch((err) => err)
}

// replace
const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function push(location, resolve, reject) {
  if (resolve || reject) return originalPush.call(this, location, resolve, reject)
  return originalReplace.call(this, location).catch((err) => err)
}

Vue.use(VueRouter)

const constantRoutes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    redirect: '/home/index',
    component: Home,
    children: [
      {
        path: '/home/index',
        name: 'Index',
        component: () => import('../views/Index/Index.vue'),
        meta: {
          keepAlive: true
        }
      },
      {
        path: '/home/follow',
        name: 'follow',
        component: () => import('../views/Follow/Follow.vue'),
        beforeEnter: (to, from, next) => {
          const isLogin = getLoginStatus()
          if (isLogin) {
            next()
          } else {
            next('/login')
          }
        }
      },
      {
        path: '/home/releaseindex',
        name: 'Releaseindex',
        component: () => import('@/views/Release/Releaseindex.vue'),
        beforeEnter: (to, from, next) => {
          const isLogin = getLoginStatus()
          if (isLogin) {
            const role = store.state.manage.role
            switch (role) {
              case 1:
                next("/renting")
                break
              case 2:
                if (_local.get("user").result.un_publish == 1) {
                  next("/myhouse")
                } else {
                  next("/releasehouse")
                }
                break
              case 3:
                if (_local.get("user").result.un_publish_transaction == 1) {
                  next("/myownerhouse")
                } else {
                  next('/buysell')
                }
                break
              case 4:
                if (_local.get("user").result.agent.vip == 2) {
                  next('/releaseagent')
                } else {
                  next("/buyjoin")
                }
                break
              case 5:
                next("/buyseek")
              default:
                next()
            }
          } else {
            next('/login')
          }
        }
      },
      {
        path: '/home/chat',
        name: 'Chat',
        component: () => import('../views/Chat/Chat.vue'),
        beforeEnter: (to, from, next) => {
          const isLogin = getLoginStatus()
          if (isLogin) {
            next()
          } else {
            next('/login')
          }
        }
      },
      {
        path: '/home/me',
        name: 'Me',
        component: () => import('../views/Me/Me.vue'),
        beforeEnter: (to, from, next) => {
          const isLogin = getLoginStatus()
          if (isLogin) {
            next()
          } else {
            next('/login')
          }
        }
      }
    ]
  },
  {
    path: '/city',
    name: 'City',
    component: () => import('../views/City.vue')
  },

  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  // {
  //   path: '/loginchoose',
  //   name: 'Loginchoose',
  //   component: () => import('../views/Loginchoose.vue')
  // },
  {
    path: '/rolechoose',
    name: 'Rolechoose',
    component: () => import('../views/Rolechoose.vue')
  },
  {
    path: '/rentindex',
    name: 'Rentindex',
    component: () => import('../views/Index/Rentindex.vue'),
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/rentscreen',
    name: 'Rentscreen',
    component: () => import('../views/Index/Rentscreen.vue'),
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/buyscreen',
    name: 'Buyscreen',
    component: () => import('../views/Index/Buyscreen.vue'),
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/buyindex',
    name: 'Buyindex',
    component: () => import('../views/Index/Buyindex.vue'),
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/map',
    name: 'Map',
    component: () => import('../views/Index/Map.vue')
  },
  {
    path: '/newslist',
    name: 'Newslist',
    component: () => import('../views/Index/Newslist.vue')
  },
  {
    path: '/newsdetail',
    name: 'Newsdetail',
    component: () => import('../views/Index/Newsdetail.vue')
  },
  {
    path: '/process',
    name: 'Process',
    component: () => import('../views/Index/Process.vue')
  },
  {
    path: '/mapapp',
    name: 'Mapapp',
    component: () => import('../views/Index/Mapapp.vue')
  },
  {
    path: '/news',
    name: 'News',
    component: () => import('../views/Index/News.vue')
  },
  {
    path: '/loading',
    name: 'Loading',
    component: () => import('../views/Index/Loading.vue'),
  },
  {
    path: '/graduation',
    name: 'Graduation',
    component: () => import('../views/Index/Graduation.vue'),
  },
  {
    path: '/announcement',
    name: 'Announcement',
    component: () => import('../views/Index/Announcement.vue'),
  },
  {
    path: '/newindex',
    name: 'Newindex',
    component: () => import('../views/New/Newindex.vue'),
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/buyhousedetail',
    name: 'Buyhousedetail',
    component: () => import('../views/Buy/Buyhousedetail.vue')
  },
  {
    path: '/buycity',
    name: 'Buycity',
    component: () => import('../views/Buy/Buycity.vue')
  },
  {
    path: '/buymap',
    name: 'Buymap',
    component: () => import('../views/Buy/Buymap.vue')
  },
  {
    path: '/buyquery',
    name: 'Buyquery',
    component: () => import('../views/Buy/Buyquery.vue'),
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/buyseek',
    name: 'Buyseek',
    component: () => import('../views/Buy/Buyseek.vue')
  },
  {
    path: '/buyseekresult',
    name: 'Buyseekresult',
    component: () => import('../views/Buy/Buyseekresult.vue')
  },
  {
    path: '/buyqueryresult',
    name: 'Buyqueryresult',
    component: () => import('../views/Buy/Buyqueryresult.vue')
  },
  {
    path: '/buyownerserve',
    name: 'Buyownerserve',
    component: () => import('../views/Buy/Buyownerserve.vue'),
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/buyevaluate',
    name: 'Buyevaluate',
    component: () => import('../views/Buy/Buyevaluate.vue')
  },
  {
    path: '/buyevaluateresult',
    name: 'Buyevaluateresult',
    component: () => import('../views/Buy/Buyevaluateresult.vue')
  },
  {
    path: '/buysell',
    name: 'Buysell',
    component: () => import('../views/Buy/Buysell.vue'),
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/buyentrust',
    name: 'Buyentrust',
    component: () => import('../views/Buy/Buyentrust.vue')
  },
  {
    path: '/buycompute',
    name: 'Buycompute',
    component: () => import('../views/Buy/Buycompute.vue')
  },
  {
    path: '/buycomputeresult',
    name: 'Buycomputeresult',
    component: () => import('../views/Buy/Buycomputeresult.vue')
  },
  {
    path: '/buyagent',
    name: 'Buyagent',
    component: () => import('../views/Buy/Buyagent.vue')
  },
  {
    path: '/buyjoin',
    name: 'Buyjoin',
    component: () => import('../views/Buy/Buyjoin.vue')
  },
  {
    path: '/buyshow',
    name: 'Buyshow',
    component: () => import('../views/Buy/Buyshow.vue')
  },
  {
    path: '/aroundprice',
    name: 'Aroundprice',
    component: () => import('../views/Buy/Aroundprice.vue')
  },
  {
    path: '/rechargeintegral',
    name: 'RechargeIntegral',
    component: () => import('../views/Buy/RechargeIntegral.vue')
  },
  {
    path: '/bond',
    name: 'Bond',
    component: () => import('../views/Buy/Bond.vue')
  },
  {
    path: '/buybond',
    name: 'BuyBond',
    component: () => import('../views/Buy/BuyBond.vue')
  },
  {
    path: '/commute',
    name: 'Commute',
    component: () => import('../views/Index/Commute.vue'),
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/commuteresult',
    name: 'Commuteresult',
    component: () => import('../views/Index/Commuteresult.vue')
  },
  {
    path: '/qa',
    name: 'Qa',
    component: () => import('../views/Index/QA.vue')
  },
  {
    path: '/qadetail',
    name: 'Qadetail',
    component: () => import('../views/Index/Qadetail.vue')
  },
  {
    path: '/housedetail',
    name: 'Housedetail',
    component: () => import('../views/Index/Housedetail.vue')
  },
  {
    path: '/sharehouse',
    name: 'Sharehouse',
    component: () => import('../views/Index/Sharehouse.vue'),
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/wholehouse',
    name: 'Wholehouse',
    component: () => import('../views/Index/Wholehouse.vue'),
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('../views/Index/Search.vue'),
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/newhousedetail',
    name: 'Newhousedetail',
    component: () => import('../views/New/Newhousedetail.vue')
  },
  {
    path: '/newscreen',
    name: 'Newscreen',
    component: () => import('../views/New/Newscreen.vue'),
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/release',
    name: 'Release',
    component: () => import('../views/Release/Release.vue')
  },
  {
    path: '/releaseagent',
    name: 'Releaseagent',
    component: () => import('../views/Release/Releaseagent.vue')
  },
  {
    path: '/releasehouse',
    name: 'ReleaseHouse',
    component: () => import('../views/Release/ReleaseHouse.vue'),
    meta: {
      keepAlive: true
    },
    beforeEnter: (to, from, next) => {
      const isLogin = getLoginStatus()
      if (isLogin) {
        next()
      } else {
        next('/login')
      }
    }
  },
  {
    path: '/upmsg',
    name: 'Upmsg',
    component: () => import('../views/Release/Upmsg.vue')
  },
  {
    path: '/upmsgshare',
    name: 'Upmsgshare',
    component: () => import('../views/Release/Upmsgshare.vue')
  },
  {
    path: '/examine',
    name: 'Examine',
    component: () => import('../views/Release/Examine.vue')
  },
  {
    path: '/renting',
    name: 'Renting',
    component: () => import('../views/Release/Renting.vue'),
    meta: {
      keepAlive: true
    },
    beforeEnter: (to, from, next) => {
      const isLogin = getLoginStatus()
      if (isLogin) {
        next()
      } else {
        next('/login')
      }
    }
  },
  {
    path: '/match',
    name: 'Match',
    component: () => import('../views/Release/Match.vue')
  },
  {
    path: '/money',
    name: 'Money',
    component: () => import('../views/Me/Money.vue')
  },
  {
    path: '/withdraw',
    name: 'Withdraw',
    component: () => import('../views/Me/Withdraw.vue')
  },
  {
    path: '/withdrawagent',
    name: 'WithDrawAgent',
    component: () => import('../views/Me/WithDrawAgent.vue')
  },
  {
    path: '/wdsuccess',
    name: 'Wdsuccess',
    component: () => import('../views/Me/Wdsuccess.vue')
  },
  {
    path: '/realname',
    name: 'Realname',
    component: () => import('../views/Me/Realname.vue')
  },
  {
    path: '/setting',
    name: 'Setting',
    component: () => import('../views/Me/Setting.vue'),
    beforeEnter: (to, from, next) => {
      if (from.path === '/login') {
        next('/home/index')
      } else {
        next()
      }
    }
  },
  {
    path: '/colorset',
    name: 'Colorset',
    component: () => import('../views/Me/Colorset.vue')
  },
  {
    path: '/newmesssage',
    name: 'Newmesssage',
    component: () => import('../views/Me/Newmesssage.vue')
  },
  {
    path: '/accsafe',
    name: 'Accsafe',
    component: () => import('../views/Me/Accsafe.vue'),
    beforeEnter: (to, from, next) => {
      if (from.path === '/login') {
        next('/home/index')
      } else {
        next()
      }
    }
  },
  {
    path: '/complaint',
    name: 'Complaint',
    component: () => import('../views/Me/Complaint.vue')
  },
  {
    path: '/today',
    name: 'Today',
    component: () => import('../views/Me/Today.vue')
  },
  {
    path: '/pushhouse',
    name: 'Pushhouse',
    component: () => import('../views/Me/Pushhouse.vue')
  },
  {
    path: '/pushguest',
    name: 'Pushguest',
    component: () => import('../views/Me/Pushguest.vue')
  },
  {
    path: '/myhouse',
    name: 'Myhouse',
    component: () => import('../views/Me/Myhouse.vue')
  },
  {
    path: '/myroom',
    name: 'Myroom',
    component: () => import('../views/Me/Myroom.vue')
  },
  {
    path: '/mybuyhouse',
    name: 'Mybuyhouse',
    component: () => import('../views/Me/Mybuyhouse.vue')
  },
  {
    path: '/myownerhouse',
    name: 'Myownerhouse',
    component: () => import('../views/Me/Myownerhouse.vue')
  },
  {
    path: '/roomchange',
    name: 'Roomchange',
    component: () => import('../views/Me/Roomchange.vue')
  },
  {
    path: '/ranthousechange',
    name: 'Ranthousechange',
    component: () => import('../views/Me/Ranthousechange.vue')
  },
  {
    path: '/blacklist',
    name: 'Blacklist',
    component: () => import('../views/Me/Blacklist.vue')
  },
  {
    path: '/phone',
    name: 'Phone',
    component: () => import('../views/Me/Phone.vue')
  },
  {
    path: '/wchat',
    name: 'Wchat',
    component: () => import('../views/Me/Wchat.vue')
  },
  {
    path: '/serve',
    name: 'Serve',
    component: () => import('../views/Me/Serve.vue')
  },
  {
    path: '/privacy',
    name: 'Privacy',
    component: () => import('../views/Me/Privacy.vue')
  },
  {
    path: '/agentagreement',
    name: 'Agentagreement',
    component: () => import('../views/Me/Agentagreement.vue')
  },
  {
    path: '/ownerupagreement',
    name: 'Ownerupagreement',
    component: () => import('../views/Me/Ownerupagreement.vue')
  },
  {
    path: '/rantagreement',
    name: 'Rantagreement',
    component: () => import('../views/Me/Rantagreement.vue')
  },
  {
    path: '/rentowneragreement',
    name: 'Rentowneragreement',
    component: () => import('../views/Me/Rentowneragreement.vue')
  },
  {
    path: '/buyagreement',
    name: 'Buyagreement',
    component: () => import('../views/Me/Buyagreement.vue')
  },
  {
    path: '/serviceguarantee',
    name: 'Serviceguarantee',
    component: () => import('../views/Me/Serviceguarantee.vue')
  },
  {
    path: '/servicerentowner',
    name: 'Servicerentowner',
    component: () => import('../views/Me/Servicerentowner.vue')
  },
  {
    path: '/myagent',
    name: 'Myagent',
    component: () => import('../views/Me/Myagent.vue')
  },
  {
    path: '/graborder',
    name: 'Graborder',
    component: () => import('../views/Me/Graborder.vue')
  },
  {
    path: '/graborderdetail',
    name: 'Graborderdetail',
    component: () => import('../views/Me/Graborderdetail.vue')
  },
  {
    path: '/serviceorderagent',
    name: 'Serviceorderagent',
    component: () => import('../views/Me/Serviceorderagent.vue')
  },
  {
    path: '/serviceorderagentdetail',
    name: 'Serviceorderagentdetail',
    component: () => import('../views/Me/Serviceorderagentdetail.vue')
  },
  {
    path: '/serviceorderbuy',
    name: 'Serviceorderbuy',
    component: () => import('../views/Me/Serviceorderbuy.vue')
  },
  {
    path: '/serviceorderbuydetail',
    name: 'Serviceorderbuydetail',
    component: () => import('../views/Me/Serviceorderbuydetail.vue')
  },
  {
    path: '/orderdetail',
    name: 'Orderdetail',
    component: () => import('../views/Me/Orderdetail.vue')
  },
  {
    path: '/servicebuylx',
    name: 'Servicebuylx',
    component: () => import('../views/Me/Servicebuylx.vue')
  },
  {
    path: '/servicebuyzm',
    name: 'Servicebuyzm',
    component: () => import('../views/Me/Servicebuyzm.vue')
  },
  {
    path: '/servicebuydk',
    name: 'Servicebuydk',
    component: () => import('../views/Me/Servicebuydk.vue')
  },
  {
    path: '/servicebuyjy',
    name: 'Servicebuyjy',
    component: () => import('../views/Me/Servicebuyjy.vue')
  },
  {
    path: '/servicebuyjr',
    name: 'Servicebuyjr',
    component: () => import('../views/Me/Servicebuyjr.vue')
  },
  {
    path: '/servicebuyjg',
    name: 'Servicebuyjg',
    component: () => import('../views/Me/Servicebuyjg.vue')
  },
  {
    path: '/getmoney',
    name: 'Getmoney',
    component: () => import('../views/Me/Getmoney.vue')
  },
  {
    path: '/getintegral',
    name: 'GetIntegral',
    component: () => import('../views/Me/GetIntegral.vue')
  },
  {
    path: '/integralrecord',
    name: 'IntegralRecord',
    component: () => import('../views/Me/IntegralRecord.vue')
  },
  {
    path: '/invitation',
    name: 'Invitation',
    component: () => import('../views/Me/Invitation.vue')
  },
  {
    path: '/rule',
    name: 'Rule',
    component: () => import('../views/Me/Rule.vue')
  },
  {
    path: '/qRcode',
    name: 'QRCode',
    component: () => import('../views/Me/QRCode.vue')
  },
  {
    path: '/share',
    name: 'Share',
    component: () => import('../views/Me/Share.vue')
  },
  {
    path: '/calendar',
    name: 'Calendar',
    component: () => import('../views/Me/Calendar.vue')
  },
  {
    path: '/ruletwo',
    name: 'RuleTwo',
    component: () => import('../views/Me/RuleTwo.vue')
  },
  {
    path: '/calendarrule',
    name: 'CalendarRule',
    component: () => import('../views/Me/CalendarRule.vue')
  },
  {
    path: '/pointorder',
    name: 'PointOrder',
    component: () => import('../views/Me/PointOrder.vue'),
  },
  {
    path: '/pointsorderdetails',
    name: 'PointsOrderDetails',
    component: () => import('../views/Me/PointsOrderDetails.vue'),
  },
  {
    path: '/agentrefund',
    name: 'AgentRefund',
    component: () => import('../views/Me/AgentRefund.vue'),
  },
  {
    path: '/roominformation',
    name: 'RoomInformation',
    component: () => import('../views/Me/RoomInformation.vue'),
  },
  {
    path: '/modifylistings',
    name: 'ModifyListings',
    component: () => import('../views/Me/ModifyListings.vue'),
  },
  {
    path: '/secondinformation',
    name: 'SecondInformation',
    component: () => import('../views/Me/SecondInformation.vue'),
  },
  {
    path: '/directconnection',
    name: 'DirectConnection',
    component: () => import('../views/Me/DirectConnection.vue'),
  },
  {
    path: '/agenthouseinfo',
    name: 'AgentHouseInfo',
    component: () => import('../views/Me/AgentHouseInfo.vue'),
  },
  {
    path: '/ownerevaluate',
    name: 'OwnerEvaluate',
    component: () => import('../views/Me/OwnerEvaluate.vue'),
  },
  {
    path: '/customersource',
    name: 'CustomerSource',
    component: () => import('../views/Me/CustomerSource.vue'),
  },
  {
    path: '/customerservice',
    name: 'Customerservice',
    component: () => import('../views/Chat/Customerservice.vue')
  },
  {
    path: '/customerservicelist',
    name: 'Customerservicelist',
    component: () => import('../views/Chat/Customerservicelist.vue')
  },
  {
    path: '/chatcontent',
    name: 'Chatcontent',
    component: () => import('../views/Chat/Chatcontent.vue'),
    beforeEnter: (to, from, next) => {
      const isLogin = getLoginStatus()
      if (isLogin) {
        next()
      } else {
        next('/login')
      }
    }
  },
  {
    path: '/notice',
    name: 'Notice',
    component: () => import('../views/Chat/Notice.vue')
  },
  {
    path: '/infocontent',
    name: 'Infocontent',
    component: () => import('../views/Chat/Infocontent.vue')
  }
]

const createRouter = () => new VueRouter({
  routes: constantRoutes
})
const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
